/**
 * Created by cailineng on 2018/5/4
 */

layui.config({
    base: '/crmCustomer/js/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
    common: 'common' //如果 mymod.js 是在根目录，也可以不用设定别名
});

$(function(){
    layui.use([ 'form', 'layer', 'table', 'element','common'], function () {
        var form = layui.form,
            table = layui.table, //表格
            $ = layui.jquery,
            element = layui.element; //元素操作
        var common = layui.common;

        //触发事件
        var active = {
            fastSearch :function () {//有点评的客户
                var param = $(this).data('method');
                common.openTab('/crmCustomer/page/queryCustomer?linkSource=customerAnalysis&tabType='+param,"layui-icon",$(this).html());
              }
         };

        //快速搜索
        $('.btnGroupDiv .layui-btn').on('click', function(){
            var othis = $(this), method = othis.data('method');
            active['fastSearch'] ? active['fastSearch'].call(this, othis) : '';
        });

        //查看全部客户按钮点击事件
        $("#findAllCustomer").click(function(){
            common.openTab('/crmCustomer/page/queryCustomer?linkSource=customerAnalysis',"layui-icon","客户");
        });

        function toCustomer(url,title){
            var createTimeBegin = $("#createTimeBegin").val()
            if(createTimeBegin!=null&&createTimeBegin!=""){
                url +="&createTimeMin="+createTimeBegin+" 00:00:00";
            }

            var createTimeEnd = $("#createTimeEnd").val();
            if(createTimeEnd!=null&&createTimeEnd!=""){
                url +="&createTimeMax="+createTimeEnd+" 23:59:59";
            }
            var belongUserId = $("#belongUserId").val();
            if(belongUserId!=null&&belongUserId!=""){
                url +="&belongUserId="+belongUserId;
            }

            url += "&linkSource=customerAnalysis";
            common.openTab(encodeURI(url),"layui-icon",title);
        }


        var stateChart = echarts.init(document.getElementById('state'));
        var intentionChart = echarts.init(document.getElementById('intention'));
        var budgetChart = echarts.init(document.getElementById('budget'));
        var budgetRentChart = echarts.init(document.getElementById('budgetRent'));
        var followChart = echarts.init(document.getElementById('follow'));
        var typeChart = echarts.init(document.getElementById('type'));
        var originChart = echarts.init(document.getElementById('origin'));


// 状态比例
        $.ajax({
            type: "POST",
            url:  '/crmCustomer/crmIndex/getStatusPercent',
            dataType: 'json',
            data: "belongUserId="+ $("#belongUserId").val()+"&createTimeBegin="+ $("#createTimeBegin").val()+"&createTimeEnd="+ $("#createTimeEnd").val(),
            success: function (data) {
                if(data==''){
                    document.getElementById('stateLi').style.display = "none";
                }
                var option = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}人 ({d}%)"
                    },
                    color: ['#f2ab00','#93ccce','#e18197'],
                    series: [
                        {
                            name:'状态比例',
                            type:'pie',
                            radius: ['40%', '55%'],
                            avoidLabelOverlap: true,
                            labelLine:{
                                normal:{
                                    length:23
                                }
                            },
                            label: {
                                normal: {
                                    formatter: function(data) {
                                        return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                                    },
                                    rich: {
                                        name: {
                                            height:18,
                                            fontSize: '13',
                                            fontWeight: 'bold'
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        },
                                        gray: {
                                            color: 'gray',
                                            height:18
                                        },
                                        red: {
                                            color: 'red',
                                            height:18
                                        }
                                    }
                                }
                            },
                            data:data
                        }
                    ]
                };
                function statusPersentClick(param) {
                    toCustomer('/crmCustomer/page/queryCustomer?status='+param.data.dataValue,param.data.name);
                }
                // 使用刚指定的配置项和数据显示图表。
                stateChart.setOption(option);
                stateChart.on("click", statusPersentClick);
            },
            error: function () {

            }
        });

// 意向比例
        $.ajax({
            type: "POST",
            url:  '/crmCustomer/crmIndex/getCustomerNeeds',
            dataType: 'json',
            data: "belongUserId="+ $("#belongUserId").val()+"&createTimeBegin="+ $("#createTimeBegin").val()+"&createTimeEnd="+ $("#createTimeEnd").val(),
            success: function (data) {
                if(data==''){
                    document.getElementById('intentionLi').style.display = "none";
                }
                var option = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}人 ({d}%)"
                    },
                    color: ['#f2ab00','#93ccce','#e18197'],
                    series: [
                        {
                            name:'意向比例',
                            type:'pie',
                            radius: ['40%', '55%'],
                            avoidLabelOverlap: true,
                            labelLine:{
                                normal:{
                                    length:24
                                }
                            },
                            label: {
                                normal: {
                                    formatter: function(data) {
                                        return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                                    },
                                    rich: {
                                        name: {
                                            height:18,
                                            fontSize: '13',
                                            fontWeight: 'bold'
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        },
                                        gray: {
                                            color: 'gray',
                                            height:18
                                        },
                                        red: {
                                            color: 'red',
                                            height:18
                                        }
                                    }
                                }
                            },
                            data:data
                        }
                    ]
                };
                function intentionChartClick(param) {
                    toCustomer('/crmCustomer/page/queryCustomer?needType='+param.data.needTypeKey,param.data.name);
                }
                // 使用刚指定的配置项和数据显示图表。
                intentionChart.setOption(option);
                intentionChart.on("click", intentionChartClick);
            },
            error: function () {

            }
        });

// 购房预算比例
        $.ajax({
            type: "POST",
            url:  '/crmCustomer/crmIndex/getBuyHouseBudgetChartData',
            dataType: 'json',
            data: "belongUserId="+ $("#belongUserId").val()+"&createTimeBegin="+ $("#createTimeBegin").val()+"&createTimeEnd="+ $("#createTimeEnd").val(),
            success: function (data) {
                if(data==''){
                    document.getElementById('budgetLi').style.display = "none";
                }
                var option = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}人 ({d}%)"
                    },
                    color: ['#9ec468','#e18197','#93ccce','#f2ab00'],
                    series: [
                        {
                            name:'购房预算比例',
                            type:'pie',
                            radius: ['40%', '55%'],
                            labelLine:{
                                normal:{
                                         length:24
                                 }
                            },
                            label: {
                                normal: {
                                    show: true,
                                    formatter: function(data) {
                                        return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                                    },
                                    rich: {
                                        name: {
                                            height:'18',
                                            fontSize: '13',
                                            fontWeight: 'bold'
                                        },
                                        gray: {
                                            color: 'gray',
                                            height:18
                                        },
                                        red: {
                                            color: 'red',
                                            height:18
                                        }
                                    }
                                }
                            },
                            data:data,
                        }
                    ]
                };
                function budgetChartClick(param) {
                    var url = '/crmCustomer/page/queryCustomer?moneyLow='+param.data.min+"&&&needType=CUSTOMER_CLAIM-01";
                    if(param.data.max!=null){
                        url+="&&moneyHigh="+param.data.max;
                    }
                    toCustomer(url,param.data.name);
                }
                // 使用刚指定的配置项和数据显示图表。
                budgetChart.setOption(option);
                budgetChart.on("click", budgetChartClick);
            },
            error: function () {

            }
        });

// 租房预算比例
        $.ajax({
            type: "POST",
            url:  '/crmCustomer/crmIndex/getRentHouseBudgetChartData',
            dataType: 'json',
            data: "belongUserId="+ $("#belongUserId").val()+"&createTimeBegin="+ $("#createTimeBegin").val()+"&createTimeEnd="+ $("#createTimeEnd").val(),
            success: function (data) {
                if(data==''){
                    document.getElementById('budgetRentLi').style.display = "none";
                }
                var option = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}人 ({d}%)"
                    },
                    color: ['#9ec468','#e18197','#93ccce','#f2ab00'],
                    series: [
                        {
                            name:'租房预算比例',
                            type:'pie',
                            radius: ['40%', '55%'],
                            avoidLabelOverlap: true,
                            labelLine:{
                                normal:{
                                    length:24
                                }
                            },
                            label: {
                                normal: {
                                    formatter: function(data) {
                                        return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                                    },
                                    rich: {
                                        name: {
                                            height:18,
                                            fontSize: '13',
                                            fontWeight: 'bold'
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        },
                                        gray: {
                                            color: 'gray',
                                            height:18
                                        },
                                        red: {
                                            color: 'red',
                                            height:18
                                        }
                                    }
                                }
                            },
                            data:data,
                        }
                    ]
                };
                function budgetRentChartClick(param) {
                    var url = '/crmCustomer/page/queryCustomer?rentMoneyLow='+param.data.min+"&&&needType=CUSTOMER_CLAIM-02";;
                    if(param.data.max!=null){
                        url+="&&rentMoneyHigh="+param.data.max;
                    }
                    toCustomer(url,param.data.name);
                }
                // 使用刚指定的配置项和数据显示图表。
                budgetRentChart.setOption(option);
                budgetRentChart.on("click", budgetRentChartClick);
            },
            error: function () {

            }
        });

        // 跟进比例
        $.ajax({
            type: "POST",
            url:  '/crmCustomer/crmIndex/getFollowPersentChartData',
            dataType: 'json',
            data: "belongUserId="+ $("#belongUserId").val()+"&createTimeBegin="+ $("#createTimeBegin").val()+"&createTimeEnd="+ $("#createTimeEnd").val(),
            success: function (data) {
                if(data==''){
                    document.getElementById('followLi').style.display = "none";
                }
                var option = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}人 ({d}%)"
                    },
                    color: ['#9ec468','#e18197','#93ccce','#f2ab00'],
                    series: [

                        {
                            name:'跟进比例',
                            type:'pie',
                            radius: ['40%', '55%'],
                            avoidLabelOverlap: true,
                            labelLine:{
                                normal:{
                                    length:24
                                }
                            },
                            label: {
                                normal: {
                                    formatter: function(data) {
                                        return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                                    },
                                    rich: {
                                        name: {
                                            height:18,
                                            fontSize: '13',
                                            fontWeight: 'bold'
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        },
                                        gray: {
                                            color: 'gray',
                                            height:18
                                        },
                                        red: {
                                            color: 'red',
                                            height:18
                                        }
                                    }
                                }
                            },
                            data:data
                        }
                    ]
                };
                function followChartClick(param) {
                    var url = '/crmCustomer/page/queryCustomer?tabType='+param.data.tagType;
                    if(param.data.tagType=="notFollowUpCustomer7Day"){
                         url += "&&notFollowDay="+param.data.notFollowDay;
                    }
                    toCustomer(url,param.data.name);
                }
                // 使用刚指定的配置项和数据显示图表。
                followChart.setOption(option);
                followChart.on("click", followChartClick);
            },
            error: function () {

            }
        });

// 成交客/欠收佣比例
        /*var option = {
            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b}: {c}人 ({d}%)"
            },
            series : [
                {
                    name: '访问来源',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '60%'],
                    avoidLabelOverlap: true,
                    label: {
                        normal: {
                            formatter: function(data) {
                                return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                            },
                            rich: {
                                name: {
                                    height:18,
                                    fontSize: '13',
                                    fontWeight: 'bold'
                                },
                                per: {
                                    color: '#eee',
                                    backgroundColor: '#334455',
                                    padding: [2, 4],
                                    borderRadius: 2
                                },
                                gray: {
                                    color: 'gray',
                                    height:18
                                },
                                red: {
                                    color: 'red',
                                    height:18
                                }
                            }
                        }
                    },
                    data:[
                        {value:251, name:'签约客',
                            itemStyle: {
                                normal: {
                                    color: '#f2ab00',
                                }
                            }
                        },
                        {value:147, name:'无收佣',
                            itemStyle: {
                                normal: {
                                    color: '#93ccce',
                                }
                            }
                        },
                        {value:102, name:'有效客',
                            itemStyle: {
                                normal: {
                                    color: '#e18197',
                                }
                            }
                        },
                    ],
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        dealChart.setOption(option);*/

// 类型比例
        $.ajax({
            type: "POST",
            url:  '/crmCustomer/crmIndex/getCustomerTypePersentChartData',
            dataType: 'json',
            data: "belongUserId="+ $("#belongUserId").val()+"&createTimeBegin="+ $("#createTimeBegin").val()+"&createTimeEnd="+ $("#createTimeEnd").val(),
            success: function (data) {
                if(data==''){
                    document.getElementById('typeLi').style.display = "none";
                }
                var option = {
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}人 ({d}%)"
                    },
                    series : [
                        {
                            name: '类型比例',
                            type: 'pie',
                            radius : '55%',
                            center: ['50%', '50%'],
                            avoidLabelOverlap: true,
                            color: ['#f2ab00','#93ccce','#e18197'],
                            labelLine:{
                                normal:{
                                    length:24
                                }
                            },
                            label: {
                                normal: {
                                    formatter: function(data) {
                                        return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                                    },
                                    rich: {
                                        name: {
                                            height:18,
                                            fontSize: '13',
                                            fontWeight: 'bold'
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        },
                                        gray: {
                                            color: 'gray',
                                            height:18
                                        },
                                        red: {
                                            color: 'red',
                                            height:18
                                        }
                                    }
                                }
                            },
                            data:data,
                        }
                    ]
                };
                function typeChartClick(param) {
                    toCustomer('/crmCustomer/page/queryCustomer?customerType='+param.data.dataValue,param.data.name);
                }
                // 使用刚指定的配置项和数据显示图表。
                typeChart.setOption(option);
                typeChart.on("click", typeChartClick);
            },
            error: function () {

            }
        });
        /*  来源比例*/
        $.ajax({
            type: "POST",
            url:  '/crmCustomer/crmIndex/getCustomerFromPersentChartData',
            dataType: 'json',
            data: "belongUserId="+ $("#belongUserId").val()+"&createTimeBegin="+ $("#createTimeBegin").val()+"&createTimeEnd="+ $("#createTimeEnd").val(),
            success: function (data) {
                if(data==''){
                    document.getElementById('originLi').style.display = "none";
                }
                // 来源比例
                var option = {
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}人 ({d}%)"
                    },
                    series : [
                        {
                            name: '来源比例',
                            type: 'pie',
                            radius : '55%',
                            center: ['50%', '50%'],
                            color: ['#f2ab00','#93ccce','#e18197'],
                            avoidLabelOverlap: true,
                            labelLine:{
                                normal:{
                                    length:24
                                }
                            },
                            label: {
                                normal: {
                                    formatter: function(data) {
                                        return  '{name|' + data.name + '}'+'\n{gray|' +data.percent + '%}\n{red|' + data.value + '}{gray|人}';
                                    },
                                    rich: {
                                        name: {
                                            height:18,
                                            fontSize: '13',
                                            fontWeight: 'bold'
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        },
                                        gray: {
                                            color: 'gray',
                                            height:18
                                        },
                                        red: {
                                            color: 'red',
                                            height:18
                                        }
                                    }
                                }
                            },
                            data:data,
                        }
                    ]
                };
                function originChartClick(param) {
                    var url = "/crmCustomer/page/queryCustomer?source=";
                    var sourceList = param.data.dataValue;
                    for(var i=0;i<sourceList.length;i++){
                        url += sourceList[i];
                        if(i!=(sourceList.length-1)){
                            url = url+",";
                        }
                    }
                   toCustomer(url,param.data.name);
                }
                // 使用刚指定的配置项和数据显示图表。
                originChart.setOption(option);
                originChart.on("click", originChartClick);
            },
            error: function () {

            }
        });
    });
});

